package com.bsg.upm.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.constant.MonitorCommonMap;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.MonitorContainerConfigService;
import com.bsg.upm.service.MonitorSubServConfigService;

/**
 * 
 * @author yucs
 *
 */

@Controller
@RequestMapping("/v1.0/monitor/subserv")
public class MonitorSubServConfigController {
	@Resource
	private MonitorContainerConfigService monitorContainerConfigService;

	@Resource
	private MonitorSubServConfigService monitorSubServConfigService;

	@RequestMapping(value = "config/{subserv_id}/metricclass-infos", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listMetricClass(@PathVariable("subserv_id") String subservId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = monitorSubServConfigService.listMetricClass(subservId);
			response.setStatus(respJson.getStatus());
		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "config/{subserv_id}/event-infos", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listEvent(@PathVariable("subserv_id") String subservId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = monitorSubServConfigService.listEvent(subservId);
			response.setStatus(respJson.getStatus());
		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "config/metricclass/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableMetricClass(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metricClass = (String) paramMap.get("metric_class");
			String subservId = (String) paramMap.get("subServ");
			String type = monitorContainerConfigService.getMetricClassType(metricClass);
			if (type == null) {
				return RespJsonFactory.build(HttpStatus.SC_GONE, "metric_class类型为null");
			}

			if (type.equals(MonitorCommonMap.CONTAINER_TYPE)) {
				respJson = monitorContainerConfigService.enableMetricClassBySubServ(subservId, metricClass);
			} else {
				respJson = monitorSubServConfigService.enableMetricClassBySubServ(subservId, metricClass);
			}

			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "config/metricclass/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson diableeMetricClass(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metricClass = (String) paramMap.get("metric_class");
			String subservId = (String) paramMap.get("subServ");
			String type = monitorContainerConfigService.getMetricClassType(metricClass);
			if (type == null) {
				return RespJsonFactory.build(HttpStatus.SC_GONE, "metric_class类型为null");
			}

			if (type.equals(MonitorCommonMap.CONTAINER_TYPE)) {
				respJson = monitorContainerConfigService.disableMetricClassBySubServ(subservId, metricClass);
			} else {
				respJson = monitorSubServConfigService.disableMetricClassBySubServ(subservId, metricClass);

			}

			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "config/event/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableEvent(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");
			String subservId = (String) paramMap.get("subServ");
			
			String type = monitorContainerConfigService.getMetricType(subservId,metric);
			if (type == null) {
				return RespJsonFactory.build(HttpStatus.SC_GONE, "未找metric的类型");
			}
			
			
			if (type.equals(MonitorCommonMap.CONTAINER_TYPE)) {
				respJson = monitorContainerConfigService.disableEventBySubServ(subservId, metric);
			} else {
				respJson = monitorSubServConfigService.disableEventBySubServ(subservId, metric, type);

			}

			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "config/event/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableEvent(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");
			String subservId = (String) paramMap.get("subServ");
			
			String type = monitorContainerConfigService.getMetricType(subservId,metric);
			if (type == null) {
				return RespJsonFactory.build(HttpStatus.SC_GONE, "未找metric的类型");
			}
			
			
			if (type.equals(MonitorCommonMap.CONTAINER_TYPE)) {
				respJson = monitorContainerConfigService.enableEventBySubServ(subservId, metric);
			} else {
				respJson = monitorSubServConfigService.enableEventBySubServ(subservId, metric, type);

			}

			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "config/event/level/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enbleEventLevel(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");
			String subservId = (String) paramMap.get("subServ");
			String level = (String) paramMap.get("level");
			
			String type = monitorContainerConfigService.getMetricType(subservId,metric);
			if (type == null) {
				return RespJsonFactory.build(HttpStatus.SC_GONE, "未找metric的类型");
			}
			
			if (type.equals(MonitorCommonMap.CONTAINER_TYPE)) {
				respJson = monitorContainerConfigService.enableEventLevelBySubServ(subservId, metric, level);
			} else {

			}

			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "config/event/level/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableEventLevel(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");
			String subservId = (String) paramMap.get("subServ");
			String level = (String) paramMap.get("level");
			
			String type = monitorContainerConfigService.getMetricType(subservId,metric);
			if (type == null) {
				return RespJsonFactory.build(HttpStatus.SC_GONE, "未找metric的类型");
			}
			

			if (type.equals(MonitorCommonMap.CONTAINER_TYPE)) {
				respJson = monitorContainerConfigService.disableEventLevelBySubServ(subservId, metric, level);
			} else {
				respJson = monitorSubServConfigService.disableEventLevelBySubServ(subservId, metric, level,type);

			}

			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	
	@RequestMapping(value = "config/event/level/update", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateEventLevel(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");
			String subservId = (String) paramMap.get("subServ");
			String level = (String) paramMap.get("level");
			String start = (String) paramMap.get("start");
			String end = (String) paramMap.get("end");
			
			String type = monitorContainerConfigService.getMetricType(subservId,metric);
			if (type == null) {
				return RespJsonFactory.build(HttpStatus.SC_GONE, "未找metric的类型");
			}
			

			if (type.equals(MonitorCommonMap.CONTAINER_TYPE)) {
				respJson = monitorContainerConfigService.updateEventLevelBySubServ(subservId, metric, level,start,end);
			} else {
				respJson = monitorSubServConfigService.updateEventLevelBySubServ(subservId, metric, level,start,end,type);

			}

			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

}